package net.xfok.switches;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;


public class ButtonBrightness extends TButton
{
	
	private final static String INTENT_ACTION = "net.xfok.switches.ButtonBrightness";
	public final static String BRIGHTNESS_CHANGED_ACTION = "net.xfok.android.state.changedBrightness";

	@Override
	public int canHandleIntent(Intent intent)
	{
		if(intent.getAction().equals(INTENT_ACTION))
			return ToggleWidget.HANDLING_TYPE_DONE;
		else if(intent.getAction().equals(BRIGHTNESS_CHANGED_ACTION))
			return ToggleWidget.HANDLING_TYPE_DONE;
		else
			return ToggleWidget.HANDLING_TYPE_UNABLE;
	}

	@Override
	public int getButtonBmp(Context context)
	{
		int brightness = Settings.System.getInt(context.getContentResolver(), "screen_brightness",128);
		if(brightness <= 1)
			return R.drawable.brightness_0;
		else if(brightness <= 64)
			return R.drawable.brightness_25;
		else if(brightness <= 128)
			return R.drawable.brightness_50;
		else if(brightness <= 192)
			return R.drawable.brightness_75;
		else return R.drawable.brightness_100;
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public void toggleSetting(Context context)
	{
		Intent intent = new Intent();
//		intent.setAction("armNclover.intent.SetBrightness");
		intent.setClassName("net.xfok.switches", "net.xfok.switches.BrightnessActivity");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//		PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		try
        {
	        pIntent.send();
        } catch (CanceledException e)
        {
	        e.printStackTrace();
        }
	}
}
